/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.lucava.boatskeeper.client.view;

import org.lucava.lib.client.view.MvcView;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import java.util.List;
import org.lucava.boatskeeper.client.event.user.CancelEditUserEvent;
import org.lucava.boatskeeper.client.event.user.SaveUserEvent;
import org.lucava.boatskeeper.shared.dto.RoleDto;
import org.lucava.boatskeeper.shared.dto.UserDto;

/**
 *
 * @author glenn
 */
public class UserEditView extends MvcView {

    private final TextBox displayName;
    private final ListBox roleBox;
    private final TextBox password;
    private final TextBox repassword;
    private final FlexTable detailsTable;
    private final Button saveButton;
    private final Button cancelButton;
    private UserDto user;
    private final Label displayNameMessage;
    private final Label passwordMessage;

    public UserEditView(EventBus eventBus) {
        super(eventBus);

        displayName = new TextBox();
        displayNameMessage = new Label();
        password = new TextBox();
        repassword = new TextBox();
        passwordMessage = new Label();
        roleBox = new ListBox();

        DecoratorPanel contentDetailsDecorator = new DecoratorPanel();
        contentDetailsDecorator.setWidth("40em");
        initWidget(contentDetailsDecorator);

        VerticalPanel contentDetailsPanel = new VerticalPanel();
        contentDetailsPanel.setWidth("100%");

        // Create the users list
        //
        detailsTable = new FlexTable();
        detailsTable.setCellSpacing(0);
        detailsTable.setWidth("100%");
        detailsTable.addStyleName("users-ListContainer");
        detailsTable.getColumnFormatter().addStyleName(1, "add-user-input");





        initDetailsTable();
        contentDetailsPanel.add(detailsTable);

        HorizontalPanel menuPanel = new HorizontalPanel();
        saveButton = new Button("Save", new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                //TODO password
                getEventBus().fireEvent(new SaveUserEvent());
            }
        });
        cancelButton = new Button("Cancel", new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                getEventBus().fireEvent(new CancelEditUserEvent());
            }
        });
        menuPanel.add(saveButton);
        menuPanel.add(cancelButton);
        contentDetailsPanel.add(menuPanel);
        contentDetailsDecorator.add(contentDetailsPanel);

    }

    private void initDetailsTable() {
        detailsTable.setWidget(0, 0, new Label("Nom"));
        detailsTable.setWidget(0, 1, displayName);
        detailsTable.setWidget(1, 0, new Label("Role"));
        detailsTable.setWidget(1, 1, roleBox);
        detailsTable.setWidget(2, 0, new Label("Votre mot de passe"));
        detailsTable.setWidget(2, 1, password);
        detailsTable.setWidget(3, 0, new Label("Confirmer votre mot de passe"));
        detailsTable.setWidget(3, 1, repassword);
        displayName.setFocus(true);
    }

    public void setUser(UserDto result) {
        this.user = result;
        displayName.setText(result.getLogin());
    }

    public void setRoles(List<RoleDto> result) {
        roleBox.clear();
        for (RoleDto role : result) {
            roleBox.addItem(role.getName(), role.getId().toString());
            if (user != null && role.getId() == user.getRole().getId()) {
                roleBox.setSelectedIndex(roleBox.getItemCount() - 1);
            }
        }
    }

    public UserDto getUser() {
        user.setLogin(displayName.getText());
        user.setPassword(displayName.getText());
        return user;
    }

    private void resetMessages() {
        displayNameMessage.setStyleName("red-text");
        displayNameMessage.setText("");
        displayNameMessage.setVisible(false);

        passwordMessage.setStyleName("red-text");
        passwordMessage.setText("");
        passwordMessage.setVisible(false);
    }

    public Boolean isValid() {

        Boolean isValid = true;

        if (!displayName.getText().matches("[0-9A-Za-z].{4,254}")) {
            displayNameMessage.setVisible(true);
            displayNameMessage.setText("login invalide");
            isValid = false;
        }

        if (!password.getText().matches("[0-9A-Za-z]{5,}")
                || !password.getText().equals(repassword.getText())) {
            passwordMessage.setVisible(true);
            passwordMessage.setText("mot de pass invalide");
            isValid = false;
        }
        return isValid;

    }
}
